package com.ezops.security;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ezops.models.Resource;

/**
 * Class builds the permission string based on {@link Shiro} permission style.
 * <p>
 * Converts the {@code List<Resource>} into {@code List<String>} of permissions.
 * 
 * <h4>Example</h4>
 * 
 * Let's say the input to this class is
 * <p>
 * (Resource Type -> Page, ResourceName -> SomeMenu, Action -> All)
 * <p>
 * The output will be (Page:SomeMenu:All)
 * 
 * @author vgaurav
 */
public abstract class PermissionBuilder {

	private static final Logger logger = LoggerFactory
			.getLogger(PermissionBuilder.class);

	/**
	 * Return the permission string.
	 * 
	 * @param resources
	 * @return
	 */
	public static List<String> buildPermissions(List<Resource> resources) {

		if (resources == null || resources.isEmpty()) {
			logger.info("The resources list is blank. Can not create permissions. ");
		}

		List<String> permissions = new ArrayList<>(resources.size());
		for (Resource resource : resources) {
			permissions.add(resource.getResourceType() + ":"
					+ resource.getId() + ":" + resource.getFileName());
		}

		return permissions;
	}

}
